<?php
namespace Api\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $_SESSION['app_name'] = '接口';
        wx_openid();
        wx_dump(wx_session());
        exit();
    }

    public function wxlogin(){
//        echo wx_access_token();
        $openid = wx_openid();
        $res = check_vip($openid);
        wx_dump($res);
        exit;
    }

    public function register(){
        register_page();
    }

    public function update_sys_cache(){
        set_time_limit(0);
        $db_dsn = 'mysql://root:@127.0.0.1:3306/haozan';
        $sqlite_dsn = 'sqlite:'.__ROOT__.'/sandcard.db';
//        'sqlite:'.ROOT_PATH.'/Data/system-data.db'
        $model = M('Huiyuan', 't_', $db_dsn);
        $sdhyModel = M('HuiyuanCredits', 't_', $db_dsn);
//        print_r($sdhyModel->select());exit;
        $data = $model->order('huiyuan_id')->where("huiyuan_id>2500")->select();
        foreach($data as $v){
            if($v['card_no'] == '') continue;
            $card  = new \Common\Model\SandCardModel($v['card_no']);
            $vipCache[$v['open_id']] = $v;
            $insertData['huiyuan_id'] = $v['huiyuan_id'];
            $insertData['open_id'] = $v['open_id'];
            $insertData['card_no'] = $v['card_no'];
            $insertData['huiyuan_name'] = $v['huiyuan_name'];
            $insertData['huiyuan_tel'] = $v['huiyuan_tel'];
            $insertData['credits'] = $card->creditsBalance();
            $sdhyModel->add($insertData, array(), true);
//            $creditsCache[$v['card_no']] = getCredits($v['card_no']);
        }
//        S('ldhusers', $vipCache);
//        S('ldhcredits', $creditsCache);
        return;
    }

    public function sysCache(){
        dump(S('ldhusers'));
        dump(S('ldhcredits'));
    }

    public function test(){
        $re = deductCredits('6001660066000000010', 20);
        echo $re;
    }

    public function jssdk(){
		echo wx_access_token();
        $signPackage = getSignPackage();
        $this->assign("signPackage", $signPackage);
        $this->display();
    }

    public function jssdk_log(){
        $access_token = wx_access_token();
        $log = date('Y-m-d H:i:s')."\r\n{$access_token}\r\n";
        $log .= var_export($_POST, true)."\r\n\r\n";
        file_put_contents('jssdk.log', $log, FILE_APPEND | LOCK_EX);
    }

    public function wximg(){
        $access_token = wx_access_token();
        $media_id = "6XC1Z-wzuzvB3H5mGUwwo8gsQBVa9Qjks9L4ueOUTJij_skX5IrdqxUhIW884Dqq";
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}";
//        $re = http_get($url);
        $re = $this->downloadWeixinFile($url);
        echo "<img src='{$re['body']}'>";
    }

    public function downloadWeixinFile($url){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_NOBODY, 0);    //只取body头
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);
        curl_close($ch);
        $imageAll = array_merge(array('header' => $httpinfo), array('body' => $package));
        return $imageAll;
    }
}